DEPTH = ../..

include $(DEPTH)/scripts/Makefile

ifeq ($(VERBOSITY), $(VERBOSITY_DEBUG))
SQLITE_VERBOSITY = --enable-debug
else
SQLITE_VERBOSITY = 
endif

all:
	@$(SCRIPTS_DIR)/progress 'Checking for outdated libsqlite3'
	@make amalgamation 2>/dev/null || /bin/sleep 0
	@make library 2>/dev/null || /bin/sleep 0

amalgamation: 
	@test sqlite/CVS -nt sqlite3.c
	@$(SCRIPTS_DIR)/progress 'Generating sqlite amalgamation (sqlite.{h,c})'
	@mkdir -p sqlite-build/
	@cd sqlite-build/ && ../sqlite/configure --disable-tcl $(SQLITE_VERBOSITY) 1>/dev/null || $(SCRIPTS_DIR)/error 'Failed configuring sqlite'
	@cd sqlite-build/ && make sqlite3.c 1>/dev/null || $(SCRIPTS_DIR)/error 'Failed generating amalgamation'
	@cp sqlite-build/sqlite3.{c,h} ./
	@rm -rf sqlite-build/

library:
	@test sqlite3.c -nt libsqlite3.a
	@$(SCRIPTS_DIR)/progress 'Building libsqlite3.a'
	@$(GCC) $(CFLAGS) -c sqlite3.c $(LDFLAGS) -o libsqlite3.a 2>/dev/null 1>/dev/null

clean:
	@$(SCRIPTS_DIR)/progress 'Cleaning'
	@rm -rf sqlite-build/ libsqlite3.a sqlite3.{c,h}
	@touch libsqlite3.a
	@sleep 1
	@touch sqlite3.c
	@sleep 1
	@touch sqlite/CVS
